#------------------------------------------------------------
# USER INPUTS
#------------------------------------------------------------

# 'tidycensus' package key for process_demographics_Nation.R
# go to https://api.census.gov/data/key_signup.html to get key
census_key <- "9a6cc5503c3c27953dafbe98ceece3fe9ea0b8ee"

current_fp <- dirname(rstudioapi::getSourceEditorContext()$path)
setwd(dirname(current_fp))

#------------------------------------------------------------
# DEPENDENCIES
#------------------------------------------------------------
library(tidycensus)
library(purrr)
library(stringr)
library(tidyverse)
library(dplyr)
library(tidyr)
library(sf)
library(tigris)
library(ggplot2)
library(grid)
library(gtable)
library(kableExtra)
library(gridExtra)
# to avoid conflicts with base packages:
library(conflicted)
conflict_prefer("filter", "dplyr")
conflict_prefer("lag", "dplyr")


# file_locations.R contains the filepaths of all raw, processed, and summarized files
# contained in a list object
source("./R/file_locations.R")

# load in all Helper functions
file.sources <- list.files(paste(file_locations$current_fp, file_locations$Helpers$`Helper Functions`, sep="/"),
                          pattern="*.R")
sapply(paste(paste(file_locations$current_fp, file_locations$Helpers$`Helper Functions`, sep="/"), 
             file.sources, sep="/"), 
       source, .GlobalEnv)

#------------------------------------------------------------
# PRE-PROCESS DATA
#------------------------------------------------------------

### General
source(paste(file_locations$current_fp, "R/process_city_geographies.R", sep="/"))
process_city_geographies(file_locations, overwrite=T)

### Chicago
source(paste(file_locations$current_fp, "R/Chicago/process_demographics_Chicago.R", sep="/"))
source(paste(file_locations$current_fp, "R/Chicago/process_comp_disadv_Chicago.R", sep="/"))

process_demographics_Chicago(file_locations, population_cutoff=200, overwrite=T)
process_comp_disadv_Chicago(file_locations, overwrite=T)

### Nation
source(paste(file_locations$current_fp, "R/Nation/process_fatal_shootings_Nation.R", sep="/"))
source(paste(file_locations$current_fp, "R/Nation/process_demographics_Nation.R", sep="/"))
source(paste(file_locations$current_fp, "R/Nation/process_comp_disadv_Nation.R", sep="/"))

process_fatal_shootings_nation(file_locations, overwrite=T)
process_demographics_nation(file_locations, population_cutoff=200, overwrite=T)
process_comp_disadv_Nation(file_locations, overwrite=T)

#------------------------------------------------------------
# RUN ANALYSIS
#------------------------------------------------------------
### Chicago
source(paste(file_locations$current_fp, "R/Chicago/Chicago_Analysis.R", sep="/"))
run_Chicago_analysis(file_locations)

### Nation
source(paste(file_locations$current_fp, "R/Nation/National_Analysis.R", sep="/"))
run_National_analysis(file_locations)

